package com.yanqu.road.server.cmd.newmonopolymkt.res;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.config.NewMonopolyMarketResConfigData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketTempPoint;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@CmdApi(reqMsg = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionReqMsg.class, respMsg = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionRespMsg.class)
@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION, desc = "资源争夺战 撤销/抢夺 位置")
public class NewMonopolyMarketResGrabActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketResGrabActionReqMsg req = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionReqMsg.parseFrom(packet.getBytes());
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!NewMonopolyMarketMgr.isSystemOpenTime(req.getType())){
            ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        if(req.getType() != eMonopolyMarketResType.RES_KING_NEW.getValue() && req.getType() != eMonopolyMarketResType.RES_QUEEN_NEW.getValue()){
            ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_TYPE_ERROR;
            return;
        }
        // 刷新玩家
        NewMonopolyMarketMgr.refreshUser(player,true,false);
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        NewMonopolyMarketProto.NewMonopolyMarketResGrabActionCrossReqMsg.Builder toCrossMsg = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionCrossReqMsg.newBuilder();
        //掠夺或者挑战
        if(req.getActionType() == 0 || req.getActionType() == 2){
            // 入座
            NewMonopolyMarketResConfigData resConfigData = NewMonopolyMarketMgr.getMonopolyMarketResConfigData(req.getType(), req.getResLevel());
            if(resConfigData == null){
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_CONFIG_NO_EXIST;
                return;
            }
            //掠夺要有搜索结果
            if(req.getActionType() == 0){
                NewMonopolyMarketTempPoint searchTemp = marketModule.userData.getSearchPointMap().get(req.getResGroupId());
                if(searchTemp == null || searchTemp.getResGroupId() != req.getResGroupId() || searchTemp.getPosIndex() != req.getPosIndex()){
                    ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
                    return;
                }
            }
            if(req.getObjectIdCount() <= 0 ){
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_NO_OBJECT;
                return;
            }
            // 派遣门客上限
            ret = NewMonopolyMarketMgr.checkDispatchNum(req.getType(), req.getObjectIdCount());
            if(ret != 0){
                return;
            }
            // 派遣队伍上限  跨服上判断
//            ret = NewMonopolyMarketMgr.checkTeamNum(player,req.getType());
//            if(ret != 0){
//                return;
//            }
            // 判断体力（挑战模式）
            if(req.getActionType() == 2 && marketModule.userData.getGuanYin() < resConfigData.getCost()){
                ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_NO_PHYSICAL;
                return;
            }
            Set<Integer> tempObjectSet = new HashSet<>();
            List<UserPatrons> patronsList = new ArrayList<>();
            List<UserBeauty> beautyList = new ArrayList<>();
            for(Integer objectId : req.getObjectIdList()){
                if(marketModule.isDispatch(req.getType(), objectId)){
                    // 派遣中
                    ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_DISPATCH_OBJECT_REPEATED;
                    return;
                }
                if(tempObjectSet.contains(objectId)){
                    ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_OBJECT_ID_IS_DUPLICATE;
                    return;
                }
                tempObjectSet.add(objectId);

                if(req.getType() == eMonopolyMarketResType.RES_KING_NEW.getValue()){
                    UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(objectId);
                    if(userPatrons == null){
                        ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_OBJECT_NOT_EXIST;
                        return;
                    }
                    patronsList.add(userPatrons);
                }else if(req.getType() == eMonopolyMarketResType.RES_QUEEN_NEW.getValue()){
                    UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(objectId);
                    if (userBeauty == null) {
                        ret = GameErrorCode.E_BEAUTY_NO_FOUND;
                        return;
                    }
                    beautyList.add(userBeauty);
                }
            }

            if(req.getType() == eMonopolyMarketResType.RES_KING_NEW.getValue()){
                // 判断门客是否存在
                for (UserPatrons item : patronsList){
                    // 门客数据 to cross
                    toCrossMsg.addPatronsList(NewMonopolyMarketMgr.builderMonopolyMarketPatronsEntity(player, item, patronsList));
                }
            }else if (req.getType() == eMonopolyMarketResType.RES_QUEEN_NEW.getValue()){

                int addPlus = NewMonopolyMarketMgr.getPatronsPlusForQueen(player);

                for (UserBeauty beauty : beautyList){
                    // 美女数据 to cross
                    toCrossMsg.addBeautyList(NewMonopolyMarketMgr.builderMonopolyMarketBeautyEntity(beauty, addPlus));
                }
            }
            toCrossMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
            if(req.getActionType() == 2) {
                //挑战需要消耗
                marketModule.resGrabNeedPhysical = resConfigData.getCost();
            }
            marketModule.level = resConfigData.getLevel();
        }else {
            // 撤位置
        }
        toCrossMsg.setCtsMsg(req);
        NewMonopolyMarketMgr.sendAppointActivityPacket(player.getUserId(), CrossProtocol.C_CROSS_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION, toCrossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            NewMonopolyMarketProto.NewMonopolyMarketResGrabActionRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION, builder);
        }
    }

}
